//个人中心
exports.user_center = function (req, res) {

    //获取restful参数
    var restfulParams = req.routeInfo.restfulParams;

    //最终结果
    var resultData = {};

    var commentCount = 0;

    var courseCount = 0;

    var cashCouponCount = 0;

    var courseCouponCount = 0;

    var exchangeCouponCount = 0;

    var browseCourseCount = 0;

    var browseActivityCount = 0;

    //任务流创建
    async.waterfall([
            //1.用户信息
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_user_info + "/u_id/" + restfulParams.u_id,
                        method: 'POST',
                        data: {
                            fields: ["u_nickname", "u_header_url", "u_sign", "u_sex"].join(",")
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.userData = data.data;
                        cb();
                    }
                );

            },
            //2.用户收藏数量
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.userSearchCollect + "/count/1/uc_uid/" + restfulParams.u_id,
                        method: 'POST',
                        data: {
                            fields: ["uc_id"].join(",")
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.userCollectCount = data.count;
                        cb();
                    }
                );

            },
            //3.用户代金券数量
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.userCashCoupon + "/ucc_is_used/0/count/1/ucc_uid/" + restfulParams.u_id,
                        method: 'POST',
                        data: {
                            fields: ["ucc_id"].join(",")
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        cashCouponCount = data.count;
                        cb();
                    }
                );

            },
            //4.用户约课劵数量
            // function (cb) {
            //     R.SEND_HTTP(
            //         req,
            //         {
            //             url: CORE_APIConfig.coreAPI.userCourseCoupon + "/count/1/ucc_uid/" + restfulParams.u_id,
            //             method: 'POST',
            //             data: {
            //                 fields: ["ucc_id"].join(",")
            //             }
            //         },
            //         function (err, data) {
            //             //判断是否有错误信息
            //             if (err) {
            //                 return cb(err, data);
            //             }
            //
            //             courseCouponCount = data.count;
            //             cb();
            //         }
            //     );
            //
            // },
            //5.用户兑换券数量
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.userExchangeCoupon + "/uec_is_used/0/count/1/uec_uid/" + restfulParams.u_id,
                        method: 'POST',
                        data: {
                            fields: ["uec_id"].join(",")
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        exchangeCouponCount = data.count;

                        //用户拥有总代金券数量
                        resultData.allCouponCount = String(parseInt(cashCouponCount) + parseInt(courseCouponCount) + parseInt(exchangeCouponCount));
                        cb();
                    }
                );

            },
            //6.用户浏览量
            /*function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchUserBrowseRecord + "/count/1/ubr_uid/" + restfulParams.u_id,
                        method: 'POST',
                        data: {
                            fields: ["ubr_id"].join(",")
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.userBrowseCount = data.count;
                        cb();
                    }
                );

            },*/

            //a.查询用户浏览课程数量
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchOrganizationCourseBrowsingHistory + "/count/1/uocbh_uid/" + restfulParams.u_id,
                        method: 'POST',
                        data: {
                            fields: ['uocbh_ocid'].join(",")
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        // resultData.browseCourse = data.data;
                        /*var browseCourseData = data.data;

                        var browseSum = 0;
                        for (var i = 0; i < browseCourseData.length; i++) {
                            browseSum += parseInt(browseCourseData[i].uocbh_count);

                        }*/
                        // var browseCourseInfo = data.count;

                        browseCourseCount = data.count;
                        cb();
                    }
                );

            },
            //b.查询用户浏览活动数量
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchOrganizationActivityBrowsingHistory + "/count/1/ubh_uid/" + restfulParams.u_id,
                        method: 'POST',
                        data: {
                            fields: ['ubh_oaid'].join(",")
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        // var browseActivityData = data.data;
                        // var browseActivityData = data.count;
                        // resultData.browseActivityData = data.data;
                        /*var browseSum = 0;
                        for (var i = 0; i < browseActivityData.length; i++) {
                            browseSum += parseInt(browseActivityData[i].ubh_count);
                        }*/
                        browseActivityCount = data.count;
                        cb();
                    }
                );

            },
            function (cb) {
                resultData.userBrowseCount = parseInt(browseCourseCount) + parseInt(browseActivityCount);
                cb();
            },
            //7.待付款订单数
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_order + "/count/1/is_hide/0/o_pay_status/0/o_uid/" + restfulParams.u_id,
                        method: 'POST',
                        data: {
                            fields: ["o_id"].join(",")
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.hasNotPayCount = data.count;
                        cb();
                    }
                );

            },
            //8.已付款未使用
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_order + "/count/1/is_hide/0/o_pay_status/1/o_uid/" + restfulParams.u_id,
                        method: 'POST',
                        data: {
                            fields: ["o_id"].join(",")
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.hasNotUsedCount = data.count;
                        cb();
                    }
                );

            },
            //8.等待评价
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_order + "/count/1/is_hide/0/o_pay_status/3/o_uid/" + restfulParams.u_id,
                        method: 'POST',
                        data: {
                            fields: ["o_id"].join(",")
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.noCommentCount = data.count;
                        cb();
                    }
                );

            },
            //退款售后
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.orderFixRefund + "/count/1/of_check_status/0/o_uid/" + restfulParams.u_id,
                        method: 'POST',
                        data: {
                            fields: ["o_id"].join(",")
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.orderRefundingCount = data.count;

                        cb();
                    }
                );

            },

        ], function (err, result) {

            //返回最终拼接结果
            RES.response(res, true, resultData);

        }
    );

}